<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Admin extends Authenticatable implements JWTSubject
{
  use Notifiable;
  protected $table = 'admins';

  //去掉我创建的数据表没有的字段
  protected $fillable = [
    'name', 'password'
  ];

  //去掉我创建的数据表没有的字段
  protected $hidden = [
    'password'
  ];
  //将密码进行加密
  public function setPasswordAttribute($value)
  {
    $this->attributes['password'] = bcrypt($value);
  }

  public function getJWTIdentifier()
  {
    return $this->getKey();
  }

  public function getJWTCustomClaims()
  {
    return [];
  }

  // 用户列表
  public static function getList($request)
  {
    $query = self::select('*');
    if (isset($request->status) && $request->status) {
      $query->where('status', $request->status);
    } 
    if (isset($request->name) && $request->name) {
      $query->where('name', 'like', '%' . $request->name . '%');
    }
    if (isset($request->sort) && $request->sort) {
      $query->orderBy($request->sort, $request->order);
    } else {
      $query->orderBy('id', 'desc');
    }
    $res = $query->paginate($request->size);
    return $res ? $res->toArray() : [];
  }
  // 用户详情-id查询
  public static function getInfo($id) {
    $res = self::select('*')->where('id', $id)->first();
    return $res ? $res->toArray() : [];
  }

  // 用户详情-名称查询
  public static function getInfoByName($name) {
    $res = self::select('id', 'name', 'status', 'role_id', 'created_at')->where('name', $name)->first();
    return $res ? $res->toArray() : [];
  }
}
